// view/my/hfjg/hfjg.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: '',
    title: "检查记录",
    huan: ['线下检查', '线上检查'],
    currtab: 0,
    page: 1,
    list: [],
    xianxia: [],
    lun: [],
    pages: [],
    lunindex: 0,
    context: null,
    zheng_wu: false,
    shujugao: '',
    start: '开始时间',
    starts: '',
    endtime: '结束时间',
    endtimes: '',
    total: '', //总条数
    inspectid: '',
    jieguolist: [{
      name: '全部',
      id: ''
    }],
    jcjie:'检查结果'
  },
  jieguo() {
    this.myRequest(yuming + 'Besall/public_class_options', {
      type_mold: 71
    }, "POST").then(res => {
      var data = res.data.data;
      var jieguolist = this.data.jieguolist;
      var aa = []
      aa = [...jieguolist, ...data]
      this.setData({
        jieguolist: aa,
      })
    })
  },
  bindjieguoaa(e) {
    console.log(e)
    var index = e.detail.value
    this.setData({
      jcjie: this.data.jieguolist[index].name,
      inspectid: this.data.jieguolist[index].id,
    })
    this.getOfflineList()
  },
  bindstart(e) {
    shuzong = []
    this.setData({
      start: e.detail.value,
      starts: e.detail.value,
      page: 1,
    })
    this.getOfflineList()
  },
  bindend(e) {
    shuzong = []
    if (new Date(e.detail.value).getTime() <= new Date(this.data.starts).getTime()) {
      wx.showToast({
        title: '结束时间不可<=开始时间',
        icon: 'none'
      })
      this.setData({
        endtime: '请选择结束时间'
      })
    } else {
      this.setData({
        endtime: e.detail.value,
        endtimes: e.detail.value,
        page: 1,
      })
      this.getOfflineList()
    }

  },
  // 查看更多
  lookmore() {
    shuzong = this.data.xianxia
    this.setData({
      page: (this.data.page) + 1
    })
    this.getOfflineList()
  },
  // 线下
  kindToggles(e) {
    const id = e.currentTarget.id
    const xianxia = this.data.xianxia
    for (let i = 0, len = xianxia.length; i < len; ++i) {
      if (xianxia[i].id == id) {
        xianxia[i].open = !xianxia[i].open
      } else {
        xianxia[i].open = false
      }
    }
    this.setData({
      xianxia
    })
  },
  kindToggle(e) {
    console.log(e)
    const id = e.currentTarget.id
    const list = this.data.list
    for (let i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list
    })
  },
  qies(e) {
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  luntab(e) {
    this.setData({
      lunindex: e.target.dataset.lunindex
    })
  },
  // 线上检查接口
  getOnlineList() {
    wx.showLoading({
      title: '加载中',
      icon: ''
    })
    var that = this
    this.myRequest(yuming + 'Administrative/adminToshopTasklist', {
      page: that.data.page,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      wx.hideLoading();
      // console.log(res)
      that.setData({
        list: res.data.data,
      })
      for (let i = 0; i < that.data.list.length; i++) {
        that.data.list[i].open = false;
      }
      // console.log(that.data.list)
    })
  },
  // 线下检查接口
  getOfflineList() {
    wx.showLoading({
      title: '加载中',
      icon: ''
    })
    var that = this
    this.myRequest(yuming + 'Administrative/adminToOfflineItems', {
      page: that.data.page,
      begintime: this.data.starts,
      endtime: this.data.endtimes,
      shopId: wx.getStorageSync('shopId'),
      inspect: that.data.inspectid
    }, "POST").then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        shu = res.data.data
        this.setData({
          total: res.data.total
        })
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              zheng_wu: true
            })
          }
        }
        that.setData({
          xianxia: shuzong.concat(shu)
        })
      }
    })
  },
  ckxq(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/my_other/xianxiaxq/xianxiaxq?id=' + id,
    })
  },
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shujugao: wx.getMenuButtonBoundingClientRect().top + 340,
      title: options.title,
      gao: wx.getMenuButtonBoundingClientRect().top + 50,
    })
   
    // const context = wx.createCanvasContext('firstCanvas');
    // context.setFillStyle('#fff')
    // context.fillRect(0, 0, 500, 150)
    // this.setData({
    //   context
    // });
    // context.draw()
  },
  // 签名
  bindtouchstart: function (e) {
    this.data.context.moveTo(e.changedTouches[0].x, e.changedTouches[0].y);
  },
  clear: function () {
    this.data.context.draw();
    this.data.context.setFillStyle('#fff')
    this.data.context.fillRect(0, 0, 500, 150)
  },
  bindtouchmove: function (e) {
    console.log(e.changedTouches)
    this.data.context.lineTo(e.changedTouches[0].x, e.changedTouches[0].y);
    this.data.context.stroke();
    this.data.context.draw(true);
    this.data.context.moveTo(e.changedTouches[0].x, e.changedTouches[0].y);
  },
  export: function () {
    // let that = this;
    // that.data.context.draw(true, wx.canvasToTempFilePath({
    //   fileType: 'jpg',
    //   canvasId: 'firstCanvas',
    //   success(res) {
    //     let { tempFilePath } = res;
    //     wx.uploadFile({
    //       filePath: tempFilePath,
    //       name: 'file',
    //       formData: {
    //         user: 'test',
    //         token: wx.getStorageSync('token')
    //       },
    //       url: app.globalData.joinApiWay + 'materials/insertfile',
    //       success(res) {
    //         let imgPath = JSON.parse(res.data).gather;
    //         wx.setStorageSync('sign', imgPath);
    //         wx.navigateBack({
    //           delta: 1,
    //         })
    //       }
    //     })
    //   },
    //   fail() {
    //     wx.showToast({
    //       title: '导出失败',
    //       icon: 'none',
    //       duration: 2000
    //     })
    //   }
    // }))
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOnlineList();
    this.getOfflineList(); //监督检查
    this.jieguo()
  },
  lower() {
    shuzong = this.data.xianxia
    if (shuzong.length >= 10 && !this.data.zheng_wu) {
      this.setData({
        page: this.data.page + 1
      })
      wx.showLoading({
        title: '加载中...'
      })
      this.getOfflineList()
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})